#pragma once
#include <array>
#include <fstream>
#include <functional>

using namespace std;

namespace U32
{
	class Statistics
	{
	private:
		array<int,1000000> ar;
	public:

		Statistics()
		{
			clear();
		}

		~Statistics()
		{
		}

		void readFromFile()
		{
			ifstream fin;
			fin.open("salary.txt");
			int kr;
			while (fin.good())
			{
				fin >> kr;
				if (kr > 20000 && kr < 1000000)
				{
					ar[kr]++;
				}
			}
		}

		void clear()
		{
			for (int i = 0; i < 1000000; i++)
			{
				ar[i] = 0;
			}
		}

		int calculate_mean()
		{
			int numberOfSalarys = 0;
			long long total = 0;
			for (int i = 0; i < 1000000; i++)
			{
				numberOfSalarys += ar[i];
				if (ar[i] > 0)
				{
					total += i * ar[i];
				}
			}

			return total / numberOfSalarys;
		}

		int calculate_median()
		{
			long long total = 0;
			for (int i = 0; i < 1000000; i++)
			{
				if (ar[i] > 0)
				{
					total += i * ar[i];
				}
			}
			long long halfTotal = total / 2;
			total = 0;
			for (int i = 0; i < 1000000; i++)
			{
				if (ar[i] > 0)
				{
					total += i * ar[i];
					if (total >= halfTotal)
						return i;
				}
			}
			return 0;
		}

		
	};
}

